package com.aso114.project.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BasesAdapter;
import com.aso114.project.bean.OrderBean;
import com.aso114.project.bean.PayNumBean;
import com.aso114.project.bean.PriceListBean;
import com.aso114.project.commonview.MyListView;
import com.aso114.project.dialog.PayDialog;
import com.aso114.project.mvp.activity.MyOrderActivity;
import com.aso114.project.mvp.activity.SelectedTopicPayActivity;
import com.aso114.project.mvp.activity.WxPayOrderActivity;
import com.aso114.project.mvp.alipay.H5PayDemoActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Helper;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderAdapter extends BasesAdapter<OrderAdapter.ViewHolder> {

    List<OrderBean> dataary;
    Context activity;
    Dialog dialog;
    private OnItemClickListener listener;
    private LayoutInflater mInflater = null;
    public OrderAdapter(Context context,List<OrderBean> araydata) {
        super(context);
        activity = context;
        dataary = araydata;
        this.mInflater = LayoutInflater.from(context);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(getInflater().inflate(R.layout.adapter_order, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder,final int position) {
        //判空
        if(dataary.get(position).getJingPinList()!=null&&dataary.get(position).getJingPinList().size()>=1){
            holder.orderTopTitle.setText(dataary.get(position).getJingPinList().get(0).getTitle());

            if(dataary.get(position).getJingPinList().get(0).getJingPinTypeId().equals("2")){
                holder.orderTopIcon.setImageResource(R.mipmap.jxtk_ya_ic);
            }else{
                holder.orderTopIcon.setImageResource(R.mipmap.jxtk_ji_ic);
            }

//                    headimg.setImageResource(R.mipmap.my_touxiang_c);
        }

        //判断状态
        if(dataary.get(position).getOrderStatus().equals("1")){
            holder.orderTopState.setText("待支付");
            holder.orderTopPrice.setText("待支付￥"+dataary.get(position).getOrderAmount());
            holder.payorder.setVisibility(View.VISIBLE);
            holder.orderDownPayTime.setVisibility(View.GONE);
        }else
        if(dataary.get(position).getOrderStatus().equals("3")){
            holder.orderTopState.setText("已支付");
            holder.orderTopPrice.setText("已支付￥"+dataary.get(position).getOrderAmount());
            holder.payorder.setVisibility(View.GONE);
            holder.orderDownPayTime.setVisibility(View.VISIBLE);
        }else {

            holder.orderTopState.setText("已失效");
            holder.payorder.setVisibility(View.GONE);
            holder.orderTopPrice.setText("待支付￥"+dataary.get(position).getOrderAmount());
            holder.orderDownPayTime.setVisibility(View.GONE);
        }

        holder.orderDownPrice.setText("￥"+dataary.get(position).getOrderAmount());
        holder.orderTopTime.setText(Helper.getymTime(Long.valueOf(dataary.get(position).getCreated())));
        holder.orderDownNumber.setText("订单编号:"+dataary.get(position).getOrderNo());
        holder.orderDownOrderTime.setText("下单时间:"+Helper.getymTime(Long.valueOf(dataary.get(position).getStartTime())));
        holder.orderDownPayTime.setText("支付时间:"+Helper.getymTime(Long.valueOf(dataary.get(position).getEndTime())));
        holder.orderDownListView.setAdapter(new OrderItemAdapter(dataary.get(position).getJingPinList()));




        holder.orderTopLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position,v);
                }

                int isVisibel =holder.orderDownLy.getVisibility();
                if(isVisibel==View.VISIBLE){
                    holder.orderDownLy.setVisibility(View.GONE);
                }else{
                    holder.orderDownLy.setVisibility(View.VISIBLE);
                }


            }
        });


        holder.payorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PayDialog dialog = new PayDialog(activity,null, new PayDialog.OnDialogClick() {
                    @Override
                    public void onZfb(PriceListBean bean) {

                        payData("1",dataary.get(position).getOrderNo());
                    }

                    @Override
                    public void onWx(PriceListBean bean) {
//                                        payData("2");
                        if(Helper.isWeixinAvilible(activity)==true){
//                            payData("2");
                            payData("2",dataary.get(position).getOrderNo());
                        }else {
                            Helper.showToast("您未安装微信,不能使用微信支付");
                        }

                    }
                });
                dialog.show();

            }
        });


    }

    @Override
    public int getItemCount() {
        return dataary.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.order_top_icon)
        ImageView orderTopIcon;
        @BindView(R.id.order_top_title)
        TextView orderTopTitle;
        @BindView(R.id.order_top_time)
        TextView orderTopTime;
        @BindView(R.id.order_top_price)
        TextView orderTopPrice;
        @BindView(R.id.order_top_state)
        TextView orderTopState;
        @BindView(R.id.order_top_ly)
        ConstraintLayout orderTopLy;
        @BindView(R.id.order_down_listView)
        MyListView orderDownListView;
        @BindView(R.id.order_down_price)
        TextView orderDownPrice;
        @BindView(R.id.order_down_number)
        TextView orderDownNumber;
        @BindView(R.id.order_down_order_time)
        TextView orderDownOrderTime;
        @BindView(R.id.payorder)
        TextView payorder;

        @BindView(R.id.order_down_pay_time)
        TextView orderDownPayTime;
        @BindView(R.id.order_down_ly)
        ConstraintLayout orderDownLy;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }



    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position,View v);
    }


    private void payData(final  String payType,String orderno) {



        if(payType.equals("1")){
            Intent intent = new Intent(activity, H5PayDemoActivity.class);

            intent.putExtra("orderNo",orderno);
            activity.startActivity(intent);
        }
        if(payType.equals("2")){

            Intent intent = new Intent(activity, WxPayOrderActivity.class);

            intent.putExtra("orderNo",orderno);
            activity.startActivity(intent);
        }


    }




}
